package com.stu.serive.impl;

import com.stu.Dto.DormitoryRepairDTO;
import com.stu.Dto.DormitoryReplyDTO;
import com.stu.Dto.NoticeDTO;
import com.stu.Dto.ViolationInformationDto;
import com.stu.context.BaseContext;
import com.stu.mapper.*;
import com.stu.pojo.*;
import com.stu.serive.AdminOperateService;
import com.stu.serive.CounsellorOperateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
@Slf4j
public class AdminOperateImpl implements AdminOperateService {

    @Autowired
    private AdminOperateMapper adminOperateMapper;
    @Autowired
    private ViolationMapper violationMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private DormitoryRepairMapper dormitoryRepairMapper;
    @Autowired
    private StuInformationMapper stuInformationMapper;

    /**
     * 审核奖学金审批
     * @param result
     */

    public void examineScholarship(Integer result, Integer id) {
        //修改学工部审核状态
        adminOperateMapper.examineScholarship(result, id);
    }



    /**
     * 审核处分解除
     * @param result
     * @param id
     */
    @Transactional
    public void examineViolation(Integer result, Integer id) {
        //修改申请表中的审核状态
        adminOperateMapper.examineViolation(result, id, BaseContext.getCurrentId());
        log.info("getCurrentId:{}",BaseContext.getCurrentId());
        //查询处分解除申请表获取外键
        ViolationApplication violationApplication = violationMapper.selectViolationApplicationById(id);
        //判断辅导员审核状态
        if(result == 1){//对处分表上的信息进行删除
            violationMapper.deleteViolation(violationApplication.getViolationId());
        }

    }

    /**
     * 发布通知
     * @param noticeDTO
     */
    public void insertNotice(NoticeDTO noticeDTO) {
        //创建实体
        Notice notice = new Notice();
        //赋值
        BeanUtils.copyProperties(noticeDTO, notice);
        notice.setEntryDate(LocalDateTime.now());

        adminOperateMapper.insertNotice(notice);
    }

    /**
     * 修改通知
     * @param noticeDTO
     */
    public void updateNotice(NoticeDTO noticeDTO) {
        //创建实体
        Notice notice = new Notice();
        //赋值
        BeanUtils.copyProperties(noticeDTO, notice);
        notice.setUpdateTime(LocalDateTime.now());

        noticeMapper.updateNotice(notice);
    }

    /**
     * 处理宿舍维修信息
     * @param dormitoryReplyDTO
     */
    public void updateDormitoryRepair(DormitoryReplyDTO dormitoryReplyDTO) {
        //创建实体
        DormitoryRepair dormitoryRepair = new DormitoryRepair();
        //赋值
        BeanUtils.copyProperties(dormitoryReplyDTO, dormitoryRepair);
        dormitoryRepair.setHandleTime(LocalDateTime.now());//处理时间
        dormitoryRepair.setReplyUser(String.valueOf(BaseContext.getCurrentId()));//处理者
        log.info("dormitoryRepair:{}",dormitoryRepair);

        dormitoryRepairMapper.updateNotice(dormitoryRepair);
    }

    /**
     * 添加学生处分信息
     * @param dto
     */
    public void addViolationInformation(ViolationInformationDto dto) {
        //创建实体
        ViolationInformation violationInformation = new ViolationInformation();
        //赋值
        BeanUtils.copyProperties(dto, violationInformation);
        violationInformation.setCreateTime(LocalDate.now());
        violationInformation.setCreateUser(String.valueOf(BaseContext.getCurrentId()));
        violationInformation.setUpdateTime(LocalDateTime.now());
        violationInformation.setUpdateUser(String.valueOf(BaseContext.getCurrentId()));

        //根据学号查询学生主键id
        Integer id =  stuInformationMapper.selectIdByStuId(dto.getStuId());
        violationInformation.setStuId(id);
        log.info("violationInformation:{}",violationInformation);

        violationMapper.addViolationInformation(violationInformation);
    }


}
